//
//  TKIMKeFuEvaluateView.m
//  test2
//
//  Created by TryHone on 17/3/29.
//  Copyright © 2017年 TryHone. All rights reserved.
//

#import "TKIMKeFuEvaluateView.h"
#import "TKIMScoreMsgView.h"


@implementation TKIMKeFuEvaluateView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.width, 40)];
        title.lineBreakMode = NSLineBreakByCharWrapping;
        title.numberOfLines = 2;
        title.font = [UIFont systemFontOfSize:16.0f];
        title.text = @"尊敬的用户,请评价下我的服务吧";
        title.textAlignment = NSTextAlignmentCenter;
        
        self.img1 = [[UIImageView alloc] initWithFrame:CGRectMake(10, title.bottom+5, 30, 30)];
        self.img1.tag = 1;
        self.img2 = [[UIImageView alloc] initWithFrame:CGRectMake(self.img1.right+5, self.img1.top, 30, 30)];
        self.img2.tag = 2;
        self.img3 = [[UIImageView alloc] initWithFrame:CGRectMake(self.img2.right+5, self.img1.top, 30, 30)];
        self.img3.tag = 3;
        self.img4 = [[UIImageView alloc] initWithFrame:CGRectMake(self.img3.right+5, self.img1.top, 30, 30)];
        self.img4.tag = 4;
        self.img5 = [[UIImageView alloc] initWithFrame:CGRectMake(self.img4.right+5, self.img1.top, 30, 30)];
        self.img5.tag = 5;
        
        self.subtitleLab = [[UILabel alloc] initWithFrame:CGRectMake(0, self.img1.bottom+8, self.width, 20)];
        self.subtitleLab.font = [UIFont systemFontOfSize:14.0f];
        self.subtitleLab.text = @"轻点星型来评分";
        self.subtitleLab.textAlignment = NSTextAlignmentCenter;

        self.sendEvaluateBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, self.subtitleLab.bottom+10, self.width, 30)];
        [self.sendEvaluateBtn setTitle:@"提交" forState:UIControlStateNormal];
        self.sendEvaluateBtn.titleLabel.textColor = [UIColor whiteColor];
        self.sendEvaluateBtn.backgroundColor = [TKIMColorHelper stringToColor:@"#0C7AFF"];
        [self.sendEvaluateBtn addTarget:self action:@selector(sendEvaluate:) forControlEvents:UIControlEventTouchUpInside];
        
        [self addSubview:title];
        
        [self addSubview:self.img1];
        [self addSubview:self.img2];
        [self addSubview:self.img3];
        [self addSubview:self.img4];
        [self addSubview:self.img5];
        
        [self addSubview:self.subtitleLab];
        [self addSubview:self.sendEvaluateBtn];
        
        [self addEvent];
    }
    return self;
}


- (void)addEvent
{
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgClick:)];
    [self.img1 addGestureRecognizer:tap];
    self.img1.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap2 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgClick:)];
    [self.img2 addGestureRecognizer:tap2];
    self.img2.userInteractionEnabled = YES;
    
    
    UITapGestureRecognizer *tap3 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgClick:)];
    [self.img3 addGestureRecognizer:tap3];
    self.img3.userInteractionEnabled = YES;
    
    
    UITapGestureRecognizer *tap4 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgClick:)];
    [self.img4 addGestureRecognizer:tap4];
    self.img4.userInteractionEnabled = YES;
    
    
    UITapGestureRecognizer *tap5 = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(imgClick:)];
    [self.img5 addGestureRecognizer:tap5];
    self.img5.userInteractionEnabled = YES;
    
    self.sendEvaluateBtn.tag=0;
    self.sendEvaluateBtn.layer.masksToBounds = YES;
    self.sendEvaluateBtn.backgroundColor = [TKUIHelper colorWithHexString:@"#2678bf"];
    self.sendEvaluateBtn.layer.cornerRadius = 5;
}


-(void)layoutSubviews
{
    [super layoutSubviews];

    id data=[[TKIMInterfaceHelper sharedInstance] valueForKeyData:TKIMSDK_KEFU_USER_DATA];
    if (data) {
        NSString* time = _dict[@"scoreTime"];
        if (time&&[@"-1" isEqualToString:time]) {
            [self  canEvaluate];
        }else{
            NSInteger newtime  = [[NSDate date] timeIntervalSince1970]*1000;
            NSInteger oldtime = [time integerValue];
            if ((oldtime-newtime)) {//可以评价的
                [self canEvaluate];
            }else{
                [self canNotEvaluate];
            }
        }
    }else{
    
        [self canNotEvaluate];
    }
}


-(void)canEvaluate
{
    NSString * scoreLevel = _dict[@"scoreLevel"];
    NSString* scoreStatus = _dict[@"scoreStatus"];

    NSInteger tag = 0;
    if (scoreLevel&&scoreLevel.length) {
        tag = [scoreLevel integerValue];
    }
    [self showStar:tag];
    
    if (scoreStatus&&[@"1" isEqualToString:scoreStatus]) {
        self.sendEvaluateBtn.hidden = YES;
        NSString* subtitle = @"非常不满意";
        switch (tag) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                subtitle = @"不满意";
                break;
            case 3:
                subtitle = @"满意";
                break;
            case 4:
                subtitle = @"很满意";
                break;
            case 5:
                subtitle = @"非常满意";
                break;
                
            default:
                break;
        }
        self.subtitleLab.text = subtitle;
        self.subtitleLab.textColor = [TKUIHelper colorWithHexString:@"#2678bf"];
        
        
        
    }else{
        self.sendEvaluateBtn.hidden = NO;
        self.sendEvaluateBtn.enabled = YES;
        self.subtitleLab.text = @"轻点星型来评分";
        self.subtitleLab.textColor = [UIColor lightGrayColor];
    }
}

-(void)canNotEvaluate
{
    NSString * scoreLevel = _dict[@"scoreLevel"];
    NSString* scoreStatus = _dict[@"scoreStatus"];

    NSInteger tag = 0;
    if (scoreLevel&&scoreLevel.length) {
        tag = [scoreLevel integerValue];
    }
    [self showStar:tag];
    
    if (scoreStatus&&[@"1" isEqualToString:scoreStatus]) {
        self.sendEvaluateBtn.hidden = YES;
        NSString* subtitle = @"非常不满意";
        switch (tag) {
            case 0:
                break;
            case 1:
                break;
            case 2:
                subtitle = @"不满意";
                break;
            case 3:
                subtitle = @"满意";
                break;
            case 4:
                subtitle = @"很满意";
                break;
            case 5:
                subtitle = @"非常满意";
                break;
                
            default:
                break;
        }
        self.subtitleLab.text = subtitle;
        self.subtitleLab.textColor = [TKUIHelper colorWithHexString:@"#2678bf"];
    }else{
        self.sendEvaluateBtn.hidden = YES;
        self.subtitleLab.text = @"轻点星型来评分";
        self.subtitleLab.textColor = [UIColor lightGrayColor];
    }
}


- (void)imgClick:(UITapGestureRecognizer*)sender
{
    id data=[[TKIMInterfaceHelper sharedInstance] valueForKeyData:TKIMSDK_KEFU_USER_DATA];
    if (data) {
        NSString* scoreStatus = _dict[@"scoreStatus"];
        if (scoreStatus&&[@"1" isEqualToString:scoreStatus]) {
            return;
        }
        UIImageView* image = (UIImageView*)sender.self.view;
        NSInteger tag = image.tag;
        self.sendEvaluateBtn.enabled = YES;
        self.sendEvaluateBtn.tag = tag;
        [self showStar:tag];
    }
}


-(void)showStar:(NSInteger)tag
{
    NSMutableArray* data = [[NSMutableArray alloc] init];
    for (int i=1; i<6; i++) {
        if (i<=tag) {
            [data addObject:@"score_img1"];
        }else{
            [data addObject:@"score_img"];
        }
    }
    
    _img1.image = TKIMGetImage(data[0]);
    _img2.image = TKIMGetImage(data[1]);
    _img3.image = TKIMGetImage(data[2]);
    _img4.image = TKIMGetImage(data[3]);
    _img5.image = TKIMGetImage(data[4]);
}


-(void)setDict:(NSDictionary *)dict
{
    _dict = dict;
    [self setNeedsLayout];
    [self setNeedsDisplay];
}


-(void)setMessage:(TKIMMessage *)message
{
    _message = message;
    [self layoutSubviews];
}


- (void)sendEvaluate:(UIButton*)sender
{
    NSString* leave=@"0";
    if (sender.tag != 0) {
        leave = [NSString stringWithFormat:@"%ld",(long)sender.tag];
        [_message.ext setValue:leave forKey:@"scoreLevel"];
        [_message.ext setValue:@"1" forKey:@"scoreStatus"];
        [self routerEventWithName:TKIMToScoreEvent userInfo:self];
    }

}


@end
